from itertools import groupby
from pydoc import describe
from collections import Counter
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.tree import plot_tree
import numpy as np
import pandas as pd
from sklearn.model_selection import cross_val_score
from matplotlib import cm
from wordcloud import STOPWORDS
import dataCleaning as dataCleaning
from sklearn.model_selection import KFold
from imblearn.pipeline import Pipeline
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import joblib
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from imblearn.over_sampling import SMOTE
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
import seaborn as sns

# 设置显示所有行和列
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
boss_fp = pd.read_csv('../data/boss_data.csv')



boss_fp['salary'] = boss_fp['salary_desc'].apply(dataCleaning.normalize_salary)
boss_fp['jobCategory'] = boss_fp['job_name'].apply(lambda x: dataCleaning.classify_job(x, dataCleaning.job_keywords))
boss_fp['min_salary'] = boss_fp['salary'].apply(dataCleaning.extract_min_salary)
boss_fp['max_salary'] = boss_fp['salary'].apply(dataCleaning.extract_max_salary)
boss_fp['avg_salary'] = boss_fp['salary'].apply(dataCleaning.extract_avg_salary)
boss_fp = boss_fp[boss_fp['jobCategory'] != 'Other']
boss_fp = boss_fp[boss_fp['jobCategory'].str.contains('Python', case=False, na=False)]

boss_fp['job_experience'] = boss_fp['job_experience'].fillna('经验不限')
boss_fp.drop_duplicates(inplace=True)

# print(boss_fp[['salary','min_salary','max_salary','avg_salary']])
boss_fp['salary_type'] = boss_fp['avg_salary'].apply(dataCleaning.extract_type_salary)

boss = boss_fp[
    ['job_name', 'city_name', 'brand_industry', 'brand_scale_name', 'job_experience', 'job_degree', 'salary_type',
     'avg_salary', 'welfare_list', 'skills']]
# print(boss.head())
# boss = boss_fp.copy()
# 自定义城市编号映射
city_mapping = {'深圳': 0, '广州': 1, '北京': 2, '厦门': 3, '杭州': 4, '成都': 5, '上海': 6}
degree_mapping = {'中专/中技/高中/大专/学历不限': 0, '本科': 1, '硕士/博士': 2}
industry_mapping = {'计算机软件': 0, '互联网': 1, '电子商务': 2, '大数据': 3, '计算机服务': 4, '人工智能': 5,
                    '智能硬件': 6, '通信/网络设备': 7, '培训/辅导机构': 8,
                    '移动互联网': 9, '游戏': 10, '进出口贸易': 11, '互联网金融': 12, '企业服务': 13, '医疗健康': 14,
                    '在线教育': 15, '信息安全': 16,
                    '计算机硬件': 17, '电子/半导体/集成电路': 18, '社交网络与媒体': 19, '半导体/芯片': 20, '咨询': 21,
                    '投资/融资': 22, '人力资源服务': 23, '其他行业': 24,
                    '电子/硬件开发': 25, '基金': 26, '生活服务(O2O)': 27, '证券/期货': 28, '运营商/增值服务': 29,
                    '文化艺术/娱乐': 30, '检测/认证/知识产权': 31, '农/林/牧/渔': 32,
                    '汽车研发/制造': 33, '电力/热力/燃气/水利': 34, '旅游/景区': 35,
                    '批发/零售': 36, '新能源': 37, '其他生活服务': 38, '港口/铁路/公路/机场': 39, '广告/公关/会展': 40,
                    '社交网络': 41, '广告营销': 42, '医疗服务': 43,
                    '医疗器械': 44, '仪器仪表/工业自动化': 45, '专利/商标/知识产权': 46,
                    '婚庆/摄影': 47, 'O2O': 48, '汽车后市场': 49,
                    '自动化设备': 50, '消费电子': 51, '新零售': 52, '生物/制药': 53, '计算机/通信/其他电子设备': 54,
                    '房地产开发经营': 55, '房屋建筑工程': 56, '日化': 57,
                    '服装/纺织': 58, '机械设备/机电/重工': 59, '法律': 60,
                    '交通/运输': 61, '装卸搬运和仓储业': 62, '物联网': 63,
                    '装修装饰': 64, '财务/审计/税务': 65, '银行': 66,
                    '工程施工': 67, '其他专业服务': 68, '其他新能源': 69, '宠物服务': 70, '旅游': 71,
                    '音乐/视频/阅读': 72, '建筑设计': 73, '广播/影视': 74, '学前教育': 75, '学术/科研': 76, '风电': 77,
                    }

# 使用 map 进行转换

le = LabelEncoder()
boss['job_name'] = le.fit_transform(boss['job_name'])
boss['city_name'] = boss['city_name'].map(city_mapping)
boss['brand_industry'] = boss['brand_industry'].map(industry_mapping)
boss['skills'] = le.fit_transform(boss['skills'])
boss['brand_scale_name'] = boss['brand_scale_name'].apply(dataCleaning.map_brand_scale)
boss['job_experience'] = boss['job_experience'].apply(dataCleaning.map_experience)
boss['job_degree'] = boss['job_degree'].apply(dataCleaning.map_degree)
# boss['salary_type'] = le.fit_transform(boss['salary_type'])
# boss['avg_salary'] = le.fit_transform(boss['avg_salary'])
# boss['welfare_list'] = le.fit_transform(boss['welfare_list'])
# 按 'product' 列进行降序排序
avg = boss['salary_type']
df1 = boss.copy()


df1 = df1.drop(columns=['salary_type','avg_salary','welfare_list']).copy()

X = df1
y = avg
# 去掉包含缺失值的行
X = X.dropna()
y = y[X.index]  # 确保 y 与 X 保持一致
# 拆分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 初始化 SMOTE
smote = SMOTE(sampling_strategy='auto', random_state=42)
# 进行过采样
X_res, y_res = smote.fit_resample(X_train, y_train)

# 初始化 KNN 分类器
# knn = KNeighborsClassifier()
# # 训练模型
# knn.fit(X_res, y_res)
# # 对测试集进行预测
# y_pred = knn.predict(X_test)
# # 输出分类报告
# print(classification_report(y_test, y_pred))
# # 输出混淆矩阵
# print(confusion_matrix(y_test, y_pred))


# 训练一个随机森林分类器
# clf = RandomForestClassifier(random_state=42)
# clf.fit(X_res, y_res)
# # 对测试集进行预测
# y_pred = clf.predict(X_test)
# # 输出分类报告
# print(classification_report(y_test, y_pred))
# # # 输出混淆矩阵
# print(confusion_matrix(y_test, y_pred))
# joblib.dump(clf, 'models/boss_randomForest_model.pkl')

# # 初始化 SMOTE
# smote = SMOTE(sampling_strategy='auto', random_state=42)
# # 进行过采样
# X_res, y_res = smote.fit_resample(X_train, y_train)
# 初始化决策树分类器
# dt = DecisionTreeClassifier(random_state=42)
# # 训练决策树模型
# dt.fit(X_res, y_res)
# # 对测试集进行预测
# y_pred = dt.predict(X_test)
# # 输出分类报告
# print(classification_report(y_test, y_pred))
# # 输出混淆矩阵
# print(confusion_matrix(y_test, y_pred))


# 使用 SMOTE 进行样本过采样
# smote = SMOTE(sampling_strategy='auto', random_state=42)
# # 初始化模型
# models = DecisionTreeClassifier(random_state=42)
# # 将 SMOTE 和模型一起放入 Pipeline 中
# pipeline = Pipeline([('smote', smote), ('models', models)])
# # 设置 KFold
# kf = KFold(n_splits=5, shuffle=True, random_state=42)
# # 进行交叉验证
# cv_scores = cross_val_score(pipeline, X, y, cv=kf, scoring='accuracy')
# print("Cross-validation scores:", cv_scores)
# print("Mean CV score:", cv_scores.mean())





# 模型名称和准确率
# plt.rcParams['font.family'] = ['STHeiti']
# models = ['KNN', '决策树', '随机森林']
# accuracies = [65.69, 77.13, 81.28]  # 相应的准确率值
#
# # 创建柱形图
# plt.figure(figsize=(8, 6))
# plt.bar(models, accuracies, color='skyblue')  # 设置柱形的颜色为skyblue
#
# # 添加标题和标签
# plt.title('模型准确率比较', fontsize=14)
# plt.xlabel('模型', fontsize=12)
# plt.ylabel('准确率 (%)', fontsize=12)
#
# # 显示准确率值在柱形上方
# for i, accuracy in enumerate(accuracies):
#     plt.text(i, accuracy + 0.2, f'{accuracy}%', ha='center', fontsize=12)
#
# # 显示图形
# plt.ylim(0, 100)  # 设置y轴的显示范围，便于查看数据
# plt.show()
